package com.whosly.strategy.registrar;

import com.whosly.strategy.spring.ManageSpringBeans;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.Order;

/**
 * ImportBeanDefinitionRegistrar的作用是在运行时自动添加Bean到已有的配置类
 *
 * @Author yueny09 <deep_blue_yang@126.com>
 * @Date 2019-09-04 14:22
 */
@Deprecated
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class StrategyRegistrar extends AbstractBeanDefinitionRegistrar {
    @Override
    protected String getTaskName() {
        return "StrategyRegistrar";
    }

    @Override
    protected void registerBeanDefinitions(AnnotationAttributes enableStrategy, BeanDefinitionRegistry registry) {
        /* 注册 ManageSpringBeans， 起到 @Bean 的作用 */
        /*
        RootBeanDefinition用来在配置阶段进行注册bean definition。
        从spring 2.5后，编写注册bean definition有了更好的的方法：GenericBeanDefinition。
        GenericBeanDefinition支持动态定义父类依赖，而非硬编码作为root bean definition。
        */
        // BeanDefinition beanDefinition = new RootBeanDefinition(ManageSpringBeans.class);
        // or
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(ManageSpringBeans.class);
        beanDefinition.setBeanClassName(ManageSpringBeans.class.getName());

        registry.registerBeanDefinition(beanDefinition.getBeanClassName(), beanDefinition);
    }
}
